package com.dd.machine.main.HardwareUtils;

import com.dd.machine.main.Interface.HardwareInterface;
import com.ensentec.Lib.CardReader;
import com.ensentec.Lib.EnsenLib;

/**
 * Created by LZX on 2017/8/1.
 */

public class CardRead {
    HardwareInterface hardwareInterface;
    private String CardNOString;


    public CardRead(HardwareInterface hardwareInterface) {
        this.hardwareInterface = hardwareInterface;
    }

    public void readCardNo(){

        hardwareInterface.onCardReadSuccess(readCard());
//        hardwareInterface.onCardReadSuccess("0000000000");
    }

    private String readCard(){
        EnsenLib.getInstance().setCardReadSuccessListener(new EnsenLib.OnCardReadSuccess() {
            @Override
            public void onCardReadSuccess(String cardNo) {
                CardNOString = cardNo;
            }
        });

        return CardNOString;
    }

    /**
     * byte[]转十六进制String
     * @param byteArray byte数组
     * @return 字符串
     */
    public static String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null){
            return null;
        }
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

}
